﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class FeedSystemCal : DrainageSystemCal
	{
		public override Result HYExecute()
		{
			HYSystemCal hysystemCal = new HYSystemCal();
			SearchSys searchSys = new SearchSys();
			string prompt = "请选择要计算系统的入口管道远端: ";
			if (!searchSys.Run(base.UiDoc, prompt))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			if (!searchSys.DumpIntoPipeSystem(out hysystemCal, base.UiDoc.Document))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			hysystemCal.systemType = HYSystemCal.SystemType.Feed;
			if (!hysystemCal.CheckSelectSystem())
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			base.GetPipeDNForPipe(hysystemCal);
			MainForm mainForm = new MainForm(base.Doc, base.UiDoc, hysystemCal);
			mainForm.Text = "给水水力计算";
			for (;;)
			{
				DialogResult dialogResult = mainForm.ShowDialog();
				if (dialogResult == DialogResult.Abort)
				{
					if (mainForm.beMinimize)
					{
						base.BeMinimizeOperator(base.UiDoc, mainForm.ElemIds);
						continue;
					}
					try
					{
						hysystemCal = new HYSystemCal();
						searchSys = new SearchSys();
						if (!searchSys.Run(base.UiDoc, prompt))
						{
							return Autodesk.Revit.UI.Result.Cancelled;
						}
						if (searchSys.DumpIntoPipeSystem(out hysystemCal, base.UiDoc.Document))
						{
							hysystemCal.systemType = HYSystemCal.SystemType.Feed;
							base.GetPipeDNForPipe(hysystemCal);
							mainForm.RefreshTheHYSystemCal(hysystemCal);
						}
						continue;
					}
					catch (Exception ex)
					{
						YJKMessageBox.Show(ex.Message);
						continue;
					}
				}
				if (dialogResult != DialogResult.OK)
				{
					break;
				}
				hysystemCal = new HYSystemCal();
				hysystemCal = mainForm.hySystemCal;
				if (base.ReturnDataToDocument(hysystemCal.PipeCals, "给水计算"))
				{
					YJKMessageBox.Information("赋回成功");
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
